//
//  TMGGodlAlertViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/8.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGGodlAlertViewController.h"

@interface TMGGodlAlertViewController ()
@property(nonatomic, strong) UITextField                  *highTextField;
@property(nonatomic, strong) UISwitch                     *highSwitch;
@property(nonatomic, strong) UITextField                  *lowTextField;
@property(nonatomic, strong) UISwitch                     *lowSwitch;
@end

@implementation TMGGodlAlertViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"金价提醒";
     [self.tableView st_autoAdjustAllResponder];
    [self configSubView];
    [self st_setRightItemWithTitle:@"保存" titleColor:[UIColor whiteColor]];
    [self sendGoldAlertRequest];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    tableHeader.backgroundColor = [UIColor whiteColor];
    
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    topView.backgroundColor = TM_orangeColor;
    [tableHeader addSubview:topView];

    UIImageView * iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 20, 42, 51)];
    iconImageView.image = [UIImage imageNamed:@"提醒"];
    [topView addSubview:iconImageView];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(iconImageView.right  +10, iconImageView.top, 100, 60)
                                                     text:@"新手特权金\n268元/克"
                                                textColor:[UIColor whiteColor]
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.numberOfLines = 0;
    titleLable.attributedText = [titleLable.text st_convertAttributeStringWithKeyWord:@"268" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25]}];
    [topView addSubview:titleLable];

    titleLable.centerX = UIScreenWidth /2;
    iconImageView.right = titleLable.left - 10;
    NSString * desStr = @"·当金价到达您设置的点位时候黄金口袋会为您托送提醒消息;\n\n·您可以自由选择同时开启两个点位提醒或值提醒单个点位;\n\n·高位之与低位值必须分别高于或者低于实时金价;\n\n";
    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(15, topView.bottom  +15, UIScreenWidth - 30, 200)
                                                     text:desStr
                                                textColor:TM_secendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [NSString lableAutoAdjustheightWithLabel:desLable];
    desLable.height = desLable.height;
//    desLable.attributedText = [desLable.text st_convertAttributeStringWithKeyWords:@[@"·",@"·",@"·"] attributes:@[@{NSForegroundColorAttributeName:TM_orangeColor},@{NSForegroundColorAttributeName:TM_orangeColor},@{NSForegroundColorAttributeName:TM_orangeColor}]];
    [tableHeader addSubview:desLable];
    
    UIView * hightView = [self headerViewWithTitle:@"金价高于或者等于"];
    hightView.top = desLable.bottom;
    [tableHeader addSubview:hightView];
    self.highTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, hightView.bottom + 20, 0.6 * UIScreenWidth, 20)];
    self.highTextField.placeholder = @"输入高位提醒值";
    self.highTextField.keyboardType = UIKeyboardTypeNumberPad;
    [tableHeader addSubview:self.highTextField];
    self.highSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 25)];
    self.highSwitch.onTintColor = TM_orangeColor;
    self.highSwitch.right = UIScreenWidth - 15;
    self.highSwitch.centerY = self.highTextField.centerY;
     [self.highSwitch addTarget:self action:@selector(onSelctedSwitch:) forControlEvents:UIControlEventValueChanged];
    [tableHeader addSubview:self.highSwitch];
    
    UIView * lowView = [self headerViewWithTitle:@"金价低于或者等于"];
    lowView.top = self.highTextField.bottom + 20;
    [tableHeader addSubview:lowView];
    self.lowTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, lowView.bottom + 20, 0.6 * UIScreenWidth, 20)];
    self.lowTextField.placeholder = @"输入低位提醒值";
    self.lowTextField.keyboardType = UIKeyboardTypeNumberPad;
    [tableHeader addSubview:self.lowTextField];
    self.lowSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 25)];
    self.lowSwitch.onTintColor = TM_orangeColor;
    self.lowSwitch.right = UIScreenWidth - 15;
    self.lowSwitch.centerY = self.lowTextField.centerY;
    [self.lowSwitch addTarget:self action:@selector(onSelctedSwitch:) forControlEvents:UIControlEventValueChanged];
    [tableHeader addSubview:self.lowSwitch];
    
    tableHeader.height = self.lowSwitch.bottom + 15;
    self.tableView.tableHeaderView = tableHeader;
}
- (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    backView.backgroundColor = self.tableView.backgroundColor;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 2* 15, 44)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [backView addSubview:titleLable];
    return backView;
}
#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
    [self sendSaveRequest];
}
- (void)onSelctedSwitch:(UISwitch*)sender{
    if (sender == self.lowSwitch) {
        if (!sender.on) {
            self.lowTextField.text = @" ";
        }
    }
    
    if (sender == self.highSwitch) {
        if (!sender.on) {
            self.highTextField.text = @" ";
        }
    }
}
#pragma mark --NetWork Method
- (void)sendGoldAlertRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/get_price_remind"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                
                                                 self.highTextField.text = responseObject[@"data"][@"high"];
                                                 self.lowTextField.text = responseObject[@"data"][@"low"];
                                                 if (self.highTextField.text.floatValue > 0.01) {
                                                     [self.highSwitch setOn:YES animated:YES];
                                                 }
                                                 if (self.lowTextField.text.floatValue > 0.01) {
                                                     [self.lowSwitch setOn:YES animated:YES];
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 //[SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendSaveRequest{
    [SVProgressHUD showWithStatus:@"保存中"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/add_gold_price"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.lowTextField.text.length) {
        [dic setObject:self.lowTextField.text forKey:@"low"];
    }
    if (self.highTextField.text.length) {
         [dic setObject:self.highTextField.text forKey:@"high"];
    }
   
    if (!self.highTextField.text.length) {
       [dic setObject:@"0" forKey:@"high"];
    }
    if (!self.lowTextField.text.length) {
        [dic setObject:@"0" forKey:@"low"];
    }
    if (!self.highSwitch.on) {
        [dic setObject:@"0" forKey:@"high"];
    }
    if (!self.lowSwitch.on) {
       [dic setObject:@"0" forKey:@"low"];
    }

    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
